/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-07-31     Administrator       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <string.h>
#include <stdio.h>
#include "lcd1602.h"
#include "multi_button.h"

extern st_btn_event btn_update_event;
extern struct rt_sensor_data sensor_data;
extern rt_thread_t lcd1602_thread;

static int lcd1602_thread_start(void);
static void lcd1602_display_entry(void *parameter);


static int lcd1602_thread_start(void)
{
    lcd1602_thread = rt_thread_create("lcd1602_thread",
                                      lcd1602_display_entry,
                                      RT_NULL,
                                      1024,
                                      10,
                                      20);
    if (lcd1602_thread != RT_NULL)
    {
        rt_thread_startup(lcd1602_thread);
    }

    return RT_EOK;
}
INIT_APP_EXPORT(lcd1602_thread_start);

static void lcd1602_display_entry(void *parameter)
{
    uint32_t count;
    rt_tick_t thread_start_tick = rt_tick_get();
    rt_tick_t update_time_old = 1000;
    unsigned char lightbuff[5];
    unsigned char * title = RT_NULL;
    unsigned char add_title[] = "SetUpdateTime:++";
    unsigned char sub_title[] = "SetUpdateTime:--";
    unsigned char updatebuff[15] = "UpdateTime:";
    unsigned char update_time_buff[6] = "1sec";

    while(1)
    {
        switch(btn_update_event.btn_disp_type)
        {
        case BTN_EVENT_POWER_ON:
            count++;

            if((update_time_old != btn_update_event.update_time_ms) ||
                (update_time_old == count * 50))
            {
                count = 0;
                update_time_old = btn_update_event.update_time_ms;
                snprintf(lightbuff, sizeof(lightbuff), "%d", sensor_data.data.light);

                lcd1602_write_str(0, 0, "LightMonitorDev");
                lcd1602_write_str(0, 1, "Light:");
                lcd1602_write_str(6, 1, "   ");
                lcd1602_write_str(6, 1, &lightbuff[0]);
                lcd1602_write_str(9, 1, "Lux");

                btn_update_event.lock = 0;
            }

            break;

        case BTN_EVENT_POWER_OFF:
            lcd1602_write_com(0x01);                    /* clr screen */
            snprintf(lightbuff, sizeof(lightbuff), "%d", sensor_data.data.light);
            count = 0;
            break;

        case BTN_EVENT_UPDATE_INC:
        case BTN_EVENT_UPDATE_SUB:
            count = 0;

            if(btn_update_event.btn_disp_type == BTN_EVENT_UPDATE_INC)
            {
                title = add_title;
            }
            else
            {
                title = sub_title;
            }

            if(btn_update_event.level == 0)
            {
                snprintf(update_time_buff, sizeof(update_time_buff), "%dsec", btn_update_event.sec);
            }else if(btn_update_event.level == 1)
            {
                snprintf(update_time_buff, sizeof(update_time_buff), "%dmin", btn_update_event.min);
            }else if(btn_update_event.level == 2)
            {
                snprintf(update_time_buff, sizeof(update_time_buff), "%dhour", btn_update_event.hour);
            }

            lcd1602_write_com(0x01);                    /* clr screen */
            lcd1602_write_str(0, 0, title);
            lcd1602_write_str(0, 1, &updatebuff[0]);
            lcd1602_write_str(11, 1, &update_time_buff);

            rt_thread_delay(1200);

            lcd1602_write_com(0x01);                    /* clr screen */
            btn_update_event.btn_disp_type = BTN_EVENT_POWER_ON;
            thread_start_tick = rt_tick_get();

            break;

        default:
            break;
        }
        rt_thread_delay_until(&thread_start_tick, 50);
    }
}

